$(document).ready(function(){

    var now = new Date();
    var timestr="";
    var daylist=["日","一","二","三","四","五","六"];
    timestr= now.getFullYear()+"年"+(now.getMonth()+1)+"月"+now.getDate()+"日  星期"+daylist[now.getDay()];
    $("#localtime").text(timestr);
   $('#stream').tinyscrollbar();
   
   $(window).on('scroll',function(e){
       var documentTop = $(window).scrollTop();
       var streamTop = 0;
       var isFixed = $("#stream").hasClass('fixed');
       streamTop = isFixed?$("#header").height():$("#stream").offset().top;
       if(streamTop <= documentTop && ! isFixed){
           
                $("#stream").addClass('fixed');
           
       }else if(streamTop > documentTop && isFixed){
                $("#stream").removeClass('fixed');
       }
   });
   $("#category-filter-wrapper").hover(function() { 
        var filterDropdown = $('#category-filter-dropdown');
	filterDropdown.is(":visible") ? filterDropdown.hide() : filterDropdown.show();
    });
    $('#latest-stream').on('click',function(event){
        $('.filter-button').removeClass('active');
        $(this).parent('li').addClass('active');
        var viewlist='';
        $.post('/web/index/latest/channel/'+$("#category-filter").attr('data-channelid'),{},function(json){
            $.each(json.data,function(i,content){
                var imgsrc = content.img==null?'http://cdn.thenextweb.com/wp-content/blogs.dir/1/files/2013/04/639846_67566584-79x66.jpg':content.img;
                viewlist += '<li data-vr-contentbox="" class="stream-item">\
                        <div class="thumb">\n\
                            <a data-post-id="605945" href="/view/articles?articleid='+content.id+'" title="'+content.description+'">\n\
                                <img width="79" height="66" src="/'+imgsrc+'" class="attachment-posts_loop wp-post-image" alt="639846_67566584" title="'+content.title+'">\n\
                            </a>\n\
                        </div>\
                        <div class="body">\
                            <div class="date" data-timestamp="1366729219"><span class="red">'+content.time+'</span> '+content.timemsg+'</div>\
                            <a class="title" href="/view/articles?articleid='+content.id+'" title="'+content.description+'">'+content.title+'</a>\
                        </div>\
                    </li>';
            });
            
            $('#stream-list ul').empty();
            $('#stream-list ul').html(viewlist);
            $('#stream').tinyscrollbar_update();
        },'json');
    });
    $('#popular-stream').on('click',function(event){
        $('.filter-button').removeClass('active');
        $(this).parent('li').addClass('active');
        var viewlist = '';
        $.post('/web/index/popular/channel/'+$("#category-filter").attr('data-channelid'),{},function(json){
            $.each(json.data,function(i,content){
                var imgsrc = content.img==null?'http://cdn.thenextweb.com/wp-content/blogs.dir/1/files/2013/04/639846_67566584-79x66.jpg':content.img;
                viewlist += '<li data-vr-contentbox="" class="stream-item">\
                        <div class="thumb">\n\
                            <a data-post-id="605945" href="/view/articles?articleid='+content.id+'" title="'+content.description+'">\n\
                                <img width="79" height="66" src="/'+imgsrc+'" class="attachment-posts_loop wp-post-image" alt="639846_67566584" title="'+content.title+'">\n\
                            </a>\n\
                        </div>\
                        <div class="body">\
                            <div class="date" data-timestamp="1366729219"><span class="red">'+content.time+'</span> '+content.timemsg+'</div>\
                            <a class="title" href="/view/articles?articleid='+content.id+'" title="'+content.description+'">'+content.title+'</a>\
                        </div>\
                    </li>';
            });
            
            $('#stream-list ul').empty();
            $('#stream-list ul').html(viewlist);
            $('#stream').tinyscrollbar_update();
        },'json');
        
    });
    $('#latest-stream').click();
});
